﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSharp.Core.Authentication;
using TSharp.Core.Authorization.Control;
using TSharp.Core.Authorization.Resources;

namespace TSharp.Core.Authorization
{
    #region 注释
    /* ====================
     * 作者：wangyao
     * 创建时间：2013/6/4 18:04:28
     * ====================
     */
    #endregion
    /// <summary>
    /// 权限判断实现类
    /// </summary>
    public class AuthorityJudgerImpl : IAuthorityJudger
    {
        /// <summary>
        /// 资源类别
        /// </summary>
        private readonly ResourceCategory category;

        private List<IResource> resources;

        /// <summary>
        /// 构造函数
        /// </summary>
        public AuthorityJudgerImpl(ResourceCategory category)
        {
            this.category = category;
        }

        /// <summary> 
        /// 判断给定的用户对给指定的资源和操作是否有操作权限。
        /// </summary>
        public bool CanAct(Guid actorId, Guid resourceKey, int operationId)
        {
            if (actorId == Guid.Parse("9AAE9961-00EC-45D5-8D5A-45808B128F46"))
            {
                return true;
            }

            var operationIds = this.category.ControlManager.GetControlData(actorId, resourceKey, false);

            return operationIds != null && operationIds.Contains(operationId);
        }
        /// <summary>
        /// Determines whether this instance [can act no inherited] the specified actor id.
        /// </summary>
        /// <param name="actorId">The actor id.</param>
        /// <param name="resourceKey">The resource key.</param>
        /// <param name="operationId">The operation id.</param>
        /// <returns><c>true</c> if this instance [can act no inherited] the specified actor id; otherwise, <c>false</c>.</returns>
        public bool CanActWithoutInherited(Guid actorId, Guid resourceKey, int operationId)
        {
            if (actorId == Guid.Parse("9AAE9961-00EC-45D5-8D5A-45808B128F46"))
            {
                return true;
            }

            var operationIds = this.category.ControlManager.GetControlData(actorId, resourceKey);

            return operationIds != null && operationIds.Contains(operationId);
        }
        /// <summary> 
        /// 根据给出的条件，判断指定的参与者是否有权将对指定的资源的指定操作授予其他参与者
        /// </summary>
        public bool CanAdmin(Guid actorId, Guid resourceKey, int operationId)
        {
            throw new NotImplementedException();
        }

        /// <summary> 
        /// 获得指定参与者对资源拥有指定操作权限的所有资源
        /// </summary>
        public List<Guid> GetCanActResourceKeys(Guid actorId, int operationId)
        {
            this.resources = new List<IResource>();

            var rootResources = this.category.GetRootResource(actorId);

            foreach (var rootResource in rootResources)
            {
                this.resources.Add(rootResource);

                GetAllResources(actorId, rootResource.Id);
            }

            var resourceIds = from resource in this.resources
                              where CanAct(actorId, resource.Id, operationId)
                              select resource.Id;

            return resourceIds.ToList();
        }

        /// <summary>
        /// 递归获得所有资源
        /// </summary>
        private void GetAllResources(Guid actorId, Guid resourceId)
        {
            var childResources = this.category.GetChildResource(actorId, resourceId);

            foreach (var resource in childResources)
            {
                this.resources.Add(resource);

                GetAllResources(actorId, resource.Id);
            }
        }

        /// <summary> 
        /// 获得指定参与者对资源拥有指定操作权限的所有资源
        /// </summary>
        public List<Guid> GetCanActResourceKeys(Guid actorId, int operationId, DateTime versionDate)
        {
            throw new NotImplementedException();
        }

        /// <summary> 
        /// 计算用户对资源的所有操作控制
        /// </summary>
        public string JudgeCanActControlData(Guid actorId, Guid resourceKey, int maxOperationId)
        {
            throw new NotImplementedException();
        }

        /// <summary> 
        /// 计算用户对资源继承得来的的所有操作控制
        /// </summary>
        public string JudgeInheritedControlData(Guid actorId, Guid resourceKey, int maxOperationId)
        {
            throw new NotImplementedException();
        }

        /// <summary> 
        /// 计算用户对资源的所有（授权）操作控制
        /// </summary>
        public string JudgeCanAdminControlData(Guid actorId, Guid resourceKey, int maxOperationId)
        {
            throw new NotImplementedException();
        }
    }
}
